<?php
namespace Home\Controller;
use Think\Controller;
/**
 * Class ArticleController
 * @package Admin\Controller
 * 2017-12-13   新闻类
 */
class NewsController extends AdminBasicController{

    public $news = '';
    public $shop_id;
    public function _initialize(){
        $this->checkLogin();
        $this->news = D('News');
        $this->shop_id = session('SHOP_ID');
    }

    /**
     * 新闻列表
     * @author mss
     * @time 2017-12-13
     */
    public function newsList(){
        $where = array();
        $param = array();
        $where['status'] = array('NEQ',9);
        $where['shop_id'] = $this->shop_id;
        $order = 'ctime DESC';
        /**按文章标题搜索*/
        if(!empty(trim(I('request.title')))){
            $title = trim(I('request.title'));
            $where['title'] = array('LIKE','%'.$title.'%');
            $param['title'] = $title;
        }

        /**按发布时间查找*/
        if(!empty(I('request.start_time')) && !empty(I('request.end_time'))){
            $start_time = I('request.start_time');
            $this->assign("start_time",$start_time);
            $end_time = I('request.end_time');
            $this->assign("end_time",$end_time);
            $where['ctime'] = array(array('EGT',strtotime($start_time)),array('ELT',strtotime($end_time)),'and');
            $parameter['start_time'] = I('request.start_time');
            $parameter['end_time'] = I('request.end_time');
        }

        $list = $this->news->selectNews($where,$order,15,$param);
        $data = $list['list'];

        $this->assign('list',$data);
        $this->assign('page',$list['page']);
        $this->assign('request',$_REQUEST);

        $this->display('newsList');
    }

    /**
     * 添加文章
     * @author mss
     * @time 2017-12-14
     */
    public function addNews(){
        if(IS_POST){
            $data = $this->news->create();
            if($data){
                if (get_magic_quotes_gpc()) {
                    $data['content'] = stripslashes($_POST['content']);
                } else {
                    $data['content'] = $_POST['content'];
                }
                $data['shop_id'] = $this->shop_id;
                $res = $this->news->addNews($data);
                if($res){
                    $this->success('添加成功',U('News/newsList'));
                }else{
                    $this->error('添加失败');
                }
            }else{
                $this->error($this->news->getError());
            }
        }else{

            $this->display('addNews');
        }

    }

    /**
     * 编辑文章
     * @author mss
     * @time 2017-11-24
     * @param art_id 文章id
     */
    public function editNews(){
        if(IS_POST){
            $data = $this->news->create();
            if($data){
                if (get_magic_quotes_gpc()) {
                    $data['content'] = stripslashes($_POST['content']);
                } else {
                    $data['content'] = $_POST['content'];
                }
                $where['news_id'] = $_POST['news_id'];
                $data['utime'] = time();
                $res = $this->news->editNews($where,$data);
                if($res){
                    $this->success('编辑成功',U('News/newsList'));
                }else{
                    $this->error('编辑失败');
                }
            }else{
                $this->error($this->news->getError());
            }
        }else{
            $id = I('news_id');
            $info = M('News')->where(array('news_id'=>$id))->find();
            $this->assign('info',$info);
            $this->display('editNews');
        }
    }

    /**
     * 删除文章
     * @author mss
     * @time 2017-11-24
     */
    public function delNews(){
        if(empty($_REQUEST['news_id'])){
            $this->error('您未选择任何操作对象');
        }
        $where['news_id'] = array('IN',I('request.news_id'));
        $data['status'] = 9;
        $data['utime'] = time();
        $upd_res = $this->news->editNews($where,$data);
        if($upd_res){
            //其他删除操作

            $this->success('删除操作成功');
        }else{
            $this->error('删除操作失败');
        }
    }

    /**
     * 修改文章显示顺序
     */
    public function ajaxUpNews(){
        $art_id = $_POST['art_id'];
        $data['sort'] = $_POST['sort'];
        $data['utime'] = time();
        $where['news_id'] = $art_id;
        $res = M('News')->where($where)->save($data);
        if($res){
            $this->ajaxReturn(1);
        }else{
            $this->ajaxReturn(0);
        }
    }
    /**
     * 编辑文章显示与否
     */
    public function ajaxStatus(){
        $id = $_POST['id'];
        $status = $_POST['status'];
        $w['news_id'] = $id;
        $data['status'] = $status;
        $data['utime'] = time();
        $res = $this->news->editNews($w,$data);
        if($res){
            $this->success('操作成功');
        }else{
            $this->error('操作失败');
        }
    }
}